#include "muduo/protobuf/dispatcher.h"
#include "muduo/protobuf/codec.h"

#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/net/TcpClient.h"

#include "request.pb.h"

class Client
{
private:
    using MessagePtr = std::shared_ptr<google::protobuf::Message>;
    using TranslateResponsePtr = std::shared_ptr<lkm::TranslateResponse>;
    using AddResponsePtr = std::shared_ptr<lkm::AddResponse>;

    muduo::CountDownLatch _latch;            // 用于实现同步
    muduo::net::EventLoopThread _threadloop; // 异步循环处理线程
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;
    ProtobufCodec _codec;           // 协议处理器
    ProtobufDispatcher _dispatcher; // 请求派发器
private:
    void OnConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected() == true){
            _conn = conn;
            _latch.countDown();
            std::cout << "与服务器建立连接 \n";
        }else{
            _conn.reset();
            std::cout << "与服务器断开连接 \n";
        }
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn,
                          const MessagePtr &message,
                          muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
    }

    void OnTranslate(const muduo::net::TcpConnectionPtr &conn,
                     const TranslateResponsePtr &message,
                     muduo::Timestamp)
    {
        std::cout << "翻译结果:" << message->msg() << "\n";
    }

    void OnAdd(const muduo::net::TcpConnectionPtr &conn,
               const AddResponsePtr &message,
               muduo::Timestamp)
    {
        std::cout << "加法结果:" << message->result() << "\n";
    }

    //这里会发生多态，父类的引用指向子类的对象
    bool send(const google::protobuf::Message &request)
    {
        if(_conn->connected() == true){
            _codec.send(_conn,request);
            return true;
        }

        return false;
    }

public:
    Client(const std::string &sip, int sport) : 
        _latch(1),
        _client(_threadloop.startLoop(), muduo::net::InetAddress(sip, sport), "client"),
        _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
        _dispatcher(std::bind(&Client::onUnknownMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        _dispatcher.registerMessageCallback<lkm::TranslateResponse>(std::bind(&Client::OnTranslate,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<lkm::AddResponse>(std::bind(&Client::OnAdd,this,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::OnConnection,this,std::placeholders::_1));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage,&_codec,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }
    void Translatesend(const std::string &msg)
    {
        lkm::TranslateRequest request;
        request.set_msg(msg);
        send(request);
    }

    void Addsend(int num1,int num2)
    {
        lkm::AddRequest request;
        request.set_num1(num1);
        request.set_num1(num2);
        send(request);
    }

    void Connect()
    {
        _client.connect();
        _latch.wait();
    }
};

int main(){

    Client client("127.0.0.1",8888);
    client.Connect();

    client.Translatesend("hello");
    client.Addsend(12,23);

    sleep(1);
    return 0;
}